/*
 * VHClubList.java
 * Created on 13 juil. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.widget;

import java.util.List;

import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Format;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.hockeo.vhweb.client.rpc.VHRpcResultReceiver;
import com.hockeo.vhweb.client.utils.VHJsData;

/**
 * List view showing a list of clubs with their logos. The list can be filled either from
 * using a list of clubs or from a list of contest history records. If the latter is used,
 * an additional information (reached contest round) is displayed.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHClubList extends LayoutContainer implements VHRpcResultReceiver<List<VHJsData>>
{
  public static final String  DATA_SOURCE_CLUBS           = "clubs";
  public static final String  DATA_SOURCE_HISTORY_RECORDS = "historyRecords";

  private ListView<VHJsData>  d_listView;
  private ListStore<VHJsData> d_store;
  private boolean             d_fDisplaysClubs            = true;

  /**
   * Creates a new club list. The data should be provided by calling
   * {@link #setRpcData(List)} after instantiation of the object.
   *
   * @param strDataSource the data source (either "clubs" or "historyRecords")
   */
  public VHClubList( String strDataSource )
  {
    d_store = new ListStore<VHJsData>();
    d_fDisplaysClubs = strDataSource.equals( DATA_SOURCE_CLUBS );

    setLayout( new FitLayout() );
    buildView();

    add( d_listView );
  }

  /**
   * Sets the data for the club list.
   *
   * @param listClubsOrHistoryRecords the list of either clubs or club contest history
   *          records
   * @param strPurpose not used
   */
  public void setRpcData( List<VHJsData> listClubsOrHistoryRecords, String strPurpose )
  {
    d_store.removeAll();
    d_store.add( listClubsOrHistoryRecords );

    if ( d_fDisplaysClubs )
      d_store.sort( "name", SortDir.ASC );
  }

  private void buildView()
  {
    d_listView = new ListView<VHJsData>() {
      @SuppressWarnings( "synthetic-access" )
      @Override
      protected VHJsData prepareData( VHJsData clubOrHistoryRecord )
      {
        String strProperty = d_fDisplaysClubs ? "name" : "club_name";

        clubOrHistoryRecord.set( "shortName", Format.ellipse( clubOrHistoryRecord.getString( strProperty ), 25 ) );
        return clubOrHistoryRecord;
      }
    };
    d_listView.setStore( d_store );
    d_listView.setTemplate( d_fDisplaysClubs ? getTemplateClub() : getTemplateHistoryRecord() );
    d_listView.setItemSelector( "div.thumb-wrap" );
    d_listView.setId( "club-list-view" );
  }

  private native String getTemplateClub()
  /*-{
  return ['<tpl for=".">',
  '<div class="thumb-wrap" id="img{id}" style="border: 1px solid white">',
  '<table>',
  '<tr>',
  '<td><div class="thumb"><img src="ImageServlet?id={image100Id}" title="{name}"></div></td>',
  '<td class="clubInfo"><img src="images/flags/32x32/{country}.png" title="{country_translated}"><br><span class="x-editable">{shortName}</span><br>{league_id_translated} ({league_category})</td>',
  '</tr></table></div>',
  '</tpl>',
  '<div class="x-clear"></div>'].join("");
  }-*/;

  private native String getTemplateHistoryRecord()
  /*-{
  return ['<tpl for=".">',
  '<div class="thumb-wrap" id="img{club_id}" style="border: 1px solid white">',
  '<table>',
  '<tr>',
  '<td><div class="thumb"><img src="ImageServlet?id={club_image100Id}" title="{name}"></div></td>',
  '<td class="clubInfo"><img src="images/flags/32x32/{club_country}.png" title="{club_country_name}"><br><span class="x-editable">{shortName}</span><br>{club_league_id_translated} ({club_league_category})<br><span style="font-style: italic;">{reachedRound_translated}</span></td>',
  '</tr></table></div>',
  '</tpl>',
  '<div class="x-clear"></div>'].join("");
  }-*/;
}
